<?php 
class MessageController extends ApiController {
	
	/**
	 * @return array action filters
	 */
	public function filters() {
		$arr = array(
			'postOnly + add'
		);
		return array_merge(parent::filters(), $arr);
	}

	public function actionAdd() {
		$model = new Message();
		$model->attributes = $_POST;

		// get user id by nickname
		$sender = User::model()->findByAttributes(array('nickname'=>$model->sent_user_id));
		$model->sent_user_id = $sender->id;

		// get user id by nickname
		$recp = User::model()->findByAttributes(array('nickname'=>$model->receive_user_id));
		$model->receive_user_id = $recp->id;

		$model->created_time = date('Y-m-d H:i:s');
		if ($model->save()) {
			$model->sender->addPoint('send_msg', false);
			$model->recp->addPoint('recv_msg', false);
			$this->_sendSuccess(1);
		} else
			$this->_sendError($model->getErrors(), 400);
	}

}